/**
 * 泛用脚本,使用2.4G内存
 */

//const logFilePath = "/hack_prepare/hack_log.txt";

export async function main(ns) {
  var targetServer = ns.args[0];
  var minSecurity = ns.getServerMinSecurityLevel(targetServer)+4;
  while(true)
  {
    var currentSecurity= ns.getServerSecurityLevel(targetServer);
    if(currentSecurity>minSecurity)
    {
      await ns.weaken(targetServer);
      //ns.write(logFilePath,"weaken","a");
    }
    else
    {
      // ---- money ----
      var MaxMoney = ns.getServerMaxMoney(targetServer)*.75;
      var AvailableMoney = ns.getServerMoneyAvailable(targetServer);
      //var MoneySource = ns.getMoneySources(targetServer);
      if(AvailableMoney<MaxMoney)
      {
        await ns.grow(targetServer);
        //ns.write(logFilePath,"grow","a");
      }
      else{
        // final:
        await ns.hack(targetServer);
        //ns.write(logFilePath,"hack","a");
      }
    }
  }
}